﻿// This file is part of My Nes
//
// A Nintendo Entertainment System / Family Computer (Nes/Famicom)
// Emulator written in C#.
// 
// Copyright © Alaa Ibrahim Hadid 2009 - 2019
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program.If not, see<http://www.gnu.org/licenses/>.
// 
// Author email: mailto:ahdsoft@online.de
//
namespace MyNes.Core
{
    [BoardInfo("Unknown", 60)]
    [HassIssues]
    class Mapper060 : Board
    {
        private int latch = 0;
        private byte menu = 0;

        internal override string Issues { get { return MNInterfaceLanguage.IssueMapper60; } }
        internal override void HardReset()
        {
            base.HardReset();
            latch = 0;
            menu = 0;
        }
        internal override void SoftReset()
        {
            base.SoftReset();
            latch = 0;
            menu = (byte)((menu + 1) & 0x3);
            Switch08KCHR(menu);
            Switch16KPRG(menu, PRGArea.Area8000);
            Switch16KPRG(menu, PRGArea.AreaC000);
        }
        internal override void WritePRG(ref ushort address, ref byte data)
        {
            latch = address & 0x100;
            Switch01KNMTFromMirroring((address & 0x8) == 0x8 ? Mirroring.Horz : Mirroring.Vert);
            Switch16KPRG((address >> 4) & ~(~address >> 7 & 0x1), PRGArea.Area8000);
            Switch16KPRG((address >> 4) | (~address >> 7 & 0x1), PRGArea.AreaC000);
            Switch08KCHR(address);
        }
        internal override void ReadPRG(ref ushort address, out byte data)
        {
            if (latch == 0)
                base.ReadPRG(ref address, out data);
            else
                data = menu;
        }
        internal override void WriteStateData(ref System.IO.BinaryWriter stream)
        {
            base.WriteStateData(ref stream);
            stream.Write(latch);
            stream.Write(menu);
        }
        internal override void ReadStateData(ref System.IO.BinaryReader stream)
        {
            base.ReadStateData(ref stream);
            latch = stream.ReadInt32();
            menu = stream.ReadByte();
        }
    }
}
